package de.tum.activities.StatsApp;



import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;

public class AveragePointsPerExerciseBarChart {
		 
	public GraphicalView execute(Context context, double[] maxPoints, double[] averagePoints) {
		
		XYMultipleSeriesRenderer renderer = buildCategoryRenderer(maxPoints.length);
		 

		XYMultipleSeriesDataset categorySeries = new XYMultipleSeriesDataset();
		CategorySeries averagePointsSeries = new CategorySeries("Average points");
		CategorySeries maxPointsSeries = new CategorySeries("Maximal points");
		for(int i=0; i<maxPoints.length; i++){
			maxPointsSeries.add("Aufgabe "+i, maxPoints[i]);
			averagePointsSeries.add("Aufgabe "+i, averagePoints[i]);
			
		}
		
		categorySeries.addSeries(maxPointsSeries.toXYSeries());
		categorySeries.addSeries(averagePointsSeries.toXYSeries());
		
		return ChartFactory.getBarChartView(context, categorySeries, renderer, Type.DEFAULT);
	}
		 
	protected XYMultipleSeriesRenderer buildCategoryRenderer(int numberOfExercises) {
		XYMultipleSeriesRenderer XYMultipleRenderer = new XYMultipleSeriesRenderer();
		XYSeriesRenderer XYRenderer = new XYSeriesRenderer();

		XYRenderer.setColor(Color.BLUE);
	    XYRenderer.setPointStyle(PointStyle.SQUARE);
	    XYRenderer.setFillBelowLine(true);
	    XYRenderer.setFillBelowLineColor(Color.WHITE);
	    XYRenderer.setFillPoints(true);
	    
		XYSeriesRenderer SecondXYRenderer = new XYSeriesRenderer();

		SecondXYRenderer.setColor(Color.GREEN);
		SecondXYRenderer.setPointStyle(PointStyle.SQUARE);
		SecondXYRenderer.setFillBelowLine(true);
		SecondXYRenderer.setFillBelowLineColor(Color.WHITE);
		SecondXYRenderer.setFillPoints(true);
	    
	    
	    XYMultipleRenderer.setBarSpacing(1);
	    XYMultipleRenderer.setXLabels(0);
	    for(int i=1; i<=numberOfExercises; i++){
	    	XYMultipleRenderer.addXTextLabel(i, "Aufgabe "+i);
	    }
	    
	    XYMultipleRenderer.addSeriesRenderer(XYRenderer);
	    XYMultipleRenderer.addSeriesRenderer(SecondXYRenderer);
	  	    
		return XYMultipleRenderer;
	}
}